package logic

import (
	"context"
	"week2/user_srv/user"

	"week2/api/internal/svc"
	"week2/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsCodeLogLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSmsCodeLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsCodeLogLogic {
	return &SmsCodeLogLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SmsCodeLogLogic) SmsCodeLog(req *types.SmsCodeLoginRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 短信验证码登录

	//判断验证码
	code, err := l.svcCtx.UserSrv.SmsCodeLog(l.ctx, &user.SmsCodeLogRequest{
		Mobile: req.Mobile,
		Code:   req.Code,
	})

	if code.Code != req.Code {
		return &types.RegMsgResponse{
			Cre:  -2,
			Msg:  "验证码错误",
			Data: nil,
		}, err
	}

	return &types.RegMsgResponse{
		Cre:  0,
		Msg:  "手机号登录成功",
		Data: nil,
	}, err
}
